<?php
namespace app\admin\logic;
use app\admin\validate\WrDataTargetMoney as WrDataTargetMoneyValidate;

class WrDataTargetMoneyLogic extends BaseLogic {

    /**
     * 获取每日数据目标金额查询条件
     * @param $department
     * @param $year
     * @return array
     */
    public function get_search_wstm_maps($department,$year){
        $maps = [];

        if($department != '全部'){
            $maps[] = ['department','=',$department];
        }
        if(!empty($year)){
            $maps[] = ['year','=',$year];
        }
        return $maps;
    }

    /**
     * 检查新增每日数据目标金额检查规则
     * @param $params
     * @return mixed
     */
    public function check_add_wdtm_rule($params){
        $validate = new WrDataTargetMoneyValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['year','=',$params['year']];
        $maps[] = ['department','=',$params['department']];
        $wdtm_detail =  model('wr_data_target_money')->get_detail($maps,'id');
        if(!empty($wdtm_detail)){
            return $this->get_response(0,'该记录已存在');
        }

        return $this->get_response(1,'OK');
    }

    /**
     * 检查修改每日数据目标金额检查规则
     * @param $params
     * @return mixed
     */
    public function check_save_wdtm_rule($params){
        $validate = new WrDataTargetMoneyValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['wdtmid']];
        $wdtm_detail = model('wr_data_target_money')->get_detail($maps,'id');
        if(empty($wdtm_detail)){
            return $this->get_response(0,'该记录不存在');
        }

        $maps = [];
        $maps[] = ['id','<>',$params['wdtmid']];
        $maps[] = ['year','=',$params['year']];
        $maps[] = ['department','=',$params['department']];
        $detail = model('wr_data_target_money')->get_detail($maps,'id');
        if(!empty($detail)){
            return $this->get_response(0,'该年份的部门目标金额已存在');
        }

        $data['wdtm_detail'] = $wdtm_detail;
        return $this->get_response(1,'OK',$data);
    }
}